package com.zx.flow.db.typehandler;

import com.zx.flow.api.execute.model.FlowOperator;
import com.zx.flow.api.util.Util;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

/**
 *
 */
public class OperatorTypeHandler extends BaseTypeHandler<FlowOperator> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, FlowOperator e, JdbcType jdbcType)
            throws SQLException {
        preparedStatement.setString(i, e == null ? "{}" : Util.getJsonStr(e));
    }

    @Override
    public FlowOperator getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String data = resultSet.getString(s);
        return resultSet.wasNull() ? null : Util.getJavaObject(data, FlowOperator.class);
    }

    @Override
    public FlowOperator getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String data = resultSet.getString(i);
        return resultSet.wasNull() ? null : Util.getJavaObject(data, FlowOperator.class);
    }

    @Override
    public FlowOperator getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String data = callableStatement.getString(i);
        return callableStatement.wasNull() ? null : Util.getJavaObject(data, FlowOperator.class);
    }
}